package com.bdqn.t350.servlet;

import com.bdqn.t350.entity.Users;
import com.bdqn.t350.service.UsersService;
import com.bdqn.t350.service.UsersServiceImp;
import com.bdqn.t350.util.Md5Util;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet(name = "LoginServlet", urlPatterns = "/LoginServlet")
public class LoginServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        String name = request.getParameter("name");
        String pwd = request.getParameter("pwd");
        String code = request.getParameter("code");

        HttpSession session = request.getSession();
        String verificationCode = (String) session.getAttribute("verificationCode");
        if (verificationCode == null || !verificationCode.equalsIgnoreCase(code)) {
            response.sendRedirect("/PetManage_war_exploded/error.jsp");
        } else {
            UsersService usersService = new UsersServiceImp();
            Users users = new Users();
            users.setUname(name);
            //设置好密码md5加密后的字符串在跟数据库里面的密码对比
            users.setUpwd(Md5Util.encrypt(pwd));
            int count = usersService.loginBiz(users);
            if (count > 0) {
                session.setAttribute("name", name);
                response.sendRedirect("/PetManage_war_exploded/user_index.jsp");
            } else {
                response.sendRedirect("/PetManage_war_exploded/error.jsp");
            }
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
